check if string contains character php

39

How do I check if a string contains a specific word php -

$a = 'How are you?';

if (strpos($a, 'are') !== false) {
    echo 'true';
}

how to check if a string contains a substring in php -

$a = 'How are you?';

if (strpos($a, 'are') !== false) {
    echo 'true';
}

check if string contains character php -

// this method is new with PHP 8 and above
$haystack = "Damn, I wonder if this string contains a comma.";
if (str_contains($haystack, ",")) {
	echo "There is a comma!!";
}

php contains substring -

<?php
$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
?>

Comments

Submit
0 Comments